//
//  MainTabBarController.m
//  HiMaster3
//
//  Created by jinghao on 15/12/22.
//  Copyright © 2015年 jinghao. All rights reserved.
//

#import "MainTabBarController.h"
#import "UIButton+Master.h"

@interface MainTabBarController ()
@property (nonatomic,strong)UIView *tabbarView;
@property (nonatomic,strong)NSArray *tabbarButton;

@end

@implementation MainTabBarController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationController.navigationBarHidden = YES;
    self.viewControllers = [self rootViewController];
}

- (NSMutableArray*)rootViewController{
    NSMutableArray* vctArray = [NSMutableArray array];
    NSMutableArray* buttonArray = [NSMutableArray array];

    UIViewController* vct = [[UIStoryboard storyboardWithName:@"NewsClue" bundle:nil] instantiateInitialViewController];
    [vctArray addObject:vct];
    
    UIButton *masterShareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [masterShareBtn setTitle:@"线索" forState:UIControlStateNormal];
    [masterShareBtn setImage:[UIImage imageNamed:@"xiansuo-gray"] forState:UIControlStateNormal];
    [masterShareBtn setImage:[UIImage imageNamed:@"xiansuo-color"] forState:UIControlStateSelected];

    [buttonArray addObject:masterShareBtn];
    
    vct = [[UIStoryboard storyboardWithName:@"HelpMe" bundle:nil] instantiateInitialViewController];
    [vctArray addObject:vct];
    
    UIButton *userShareBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [userShareBtn setTitle:@"求助" forState:UIControlStateNormal];
    [userShareBtn setImage:[UIImage imageNamed:@"qiuzhu-gray"] forState:UIControlStateNormal];
    [userShareBtn setImage:[UIImage imageNamed:@"qiuzhu-color"] forState:UIControlStateSelected];
    [buttonArray addObject:userShareBtn];
    
    
    UIButton *shouhuBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [shouhuBtn setTitle:@"守护" forState:UIControlStateNormal];
    [shouhuBtn setImage:[UIImage imageNamed:@"shouhu-gray"] forState:UIControlStateNormal];
    [shouhuBtn setImage:[UIImage imageNamed:@"shouhu-color"] forState:UIControlStateSelected];
    [buttonArray addObject:shouhuBtn];
    
    vct = [[UIStoryboard storyboardWithName:@"GuardMe" bundle:nil] instantiateInitialViewController];
    [vctArray addObject:vct];
    
    self.tabbarButton = buttonArray;
    
//    self.tabBar.tintColor = [UIColor blackColor];
//    self.tabBar.barTintColor = [UIColor blackColor];
//    for(int i=0;i<vctArray.count;i++){
//        UITabBarItem *item = nil;
//        if (i==0) {
//            item = [[UITabBarItem alloc] initWithTitle:@"达人" image:[UIImage imageNamed:@"Master_bar_nor"] selectedImage:[UIImage imageNamed:@"Master_bar_selected"]];
//        }else if (i==1) {
//            item = [[UITabBarItem alloc] initWithTitle:@"牛人" image:[UIImage imageNamed:@"Master_bar_nor"] selectedImage:[UIImage imageNamed:@"Master_bar_selected"]];
//        }
//        ((UIViewController*)vctArray[i]).tabBarItem = item;
//    }
    
//    [self.tabBar addSubview:self.tabbarView];
//    self.tabbarView.frame = self.tabBar.bounds; //CGRectMake(0, 0, ScreenWidth, 49);
    float btnW = ScreenWidth/self.tabbarButton.count;
    for (int i=0; i<self.tabbarButton.count; i++) {
        UIButton *btn = self.tabbarButton[i];
        btn.adjustsImageWhenHighlighted = NO;
        btn.titleLabel.font = [UIFont systemFontOfSize:10];
        
        [btn setTitleColor:[UIColor colorWithHex:0x777777] forState:UIControlStateNormal];
        [btn setTitleColor:[UIColor colorWithHex:0x21c7b6] forState:UIControlStateSelected];
        
        [btn setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithHex:0x21252b]] forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageWithColor:AppDefaultColor] forState:UIControlStateSelected];
        
//        btn.imageEdgeInsets = UIEdgeInsetsMake(0, -15, 0, 15);//图片和文字左右排列
        
        [btn addTarget:self action:@selector(tabbarButtonOnClick:) forControlEvents:UIControlEventTouchUpInside];
        btn.frame = CGRectMake(i*btnW, 0, btnW, self.tabBar.height);
        
        [btn centerImageAndTitle:2.0];//垂直居中
        
        [self.tabbarView addSubview:btn];
        if (i==0) {
            btn.selected = YES;
        }
    }
    return vctArray;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

-(UIInterfaceOrientationMask)supportedInterfaceOrientations
{
    return UIInterfaceOrientationMaskAll;
}

- (UIView*)tabbarView
{
    if (!_tabbarView) {
        _tabbarView = [[UIView alloc] init];
        _tabbarView.backgroundColor = [UIColor redColor];
//        [self.tabBar addSubview:_tabbarView];
//        
//        //logoImageView左侧与父视图左侧对齐
//        NSLayoutConstraint* leftConstraint = [NSLayoutConstraint constraintWithItem:_tabbarView attribute:NSLayoutAttributeLeading relatedBy:NSLayoutRelationEqual toItem:self.tabBar attribute:NSLayoutAttributeLeading multiplier:1.0f constant:0.0f];
//        
//        //logoImageView右侧与父视图右侧对齐
//        NSLayoutConstraint* rightConstraint = [NSLayoutConstraint constraintWithItem:_tabbarView attribute:NSLayoutAttributeTrailing relatedBy:NSLayoutRelationEqual toItem:self.tabBar attribute:NSLayoutAttributeTrailing multiplier:1.0f constant:0.0f];
//        
//        //logoImageView顶部与父视图顶部对齐
//        NSLayoutConstraint* topConstraint = [NSLayoutConstraint constraintWithItem:_tabbarView attribute:NSLayoutAttributeTop relatedBy:NSLayoutRelationEqual toItem:self.tabBar attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
//        
//        //logoImageView底部与父视图顶部对齐
//        NSLayoutConstraint* bottompConstraint = [NSLayoutConstraint constraintWithItem:_tabbarView attribute:NSLayoutAttributeBottom relatedBy:NSLayoutRelationEqual toItem:self.tabBar attribute:NSLayoutAttributeTop multiplier:1.0f constant:0.0f];
        
//        //iOS 6.0或者7.0调用addConstraints
//        if (SystemVerison<8.0) {
//            [self.tabBar addConstraints:@[leftConstraint, rightConstraint, topConstraint, bottompConstraint]];
//        }else{
//            leftConstraint.active = YES;
//            rightConstraint.active = YES;
//            topConstraint.active = YES;
//            bottompConstraint.active = YES;
//        }
        
        //iOS 8.0以后设置active属性值
        
    }
    return _tabbarView;
}

// New Autorotation support.
- (BOOL)shouldAutorotate{
    return TRUE;
}


- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
    if (self.tabbarView.superview==nil) {
        [self.tabBar addSubview:self.tabbarView];
        self.tabbarView.frame = self.tabBar.bounds; //CGRectMake(0, 0, ScreenWidth, 49);
    }
    
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        [self performSegueWithIdentifier:@"segue_LaunchScreen" sender:nil];
//    });
}

- (void)viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear:animated];
}

- (void)tabbarButtonOnClick:(UIButton*)button{
    NSInteger selectIndex = [self.tabbarButton indexOfObject:button];
    if (selectIndex != NSNotFound && selectIndex != self.selectedIndex) {
        self.selectedIndex = selectIndex;
        for (UIButton *btn in self.tabbarButton) {
            btn.selected = NO;
        }
        button.selected = YES;
    }
}


#pragma  mark --

- (BOOL)slideNavigationControllerShouldDisplayRightMenu{
    return NO;
}

- (BOOL)slideNavigationControllerShouldDisplayLeftMenu{
    return NO;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
